package com.china08.yxyapi.repository.db;

import java.util.List;
import java.util.Set;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.china08.yxyapi.entity.db.res.YxyResBook;

public interface YxyResBookRepository extends JpaRepository<YxyResBook, String> {

    List<YxyResBook> findBySubjectIdAndVersionIdAndPhaseIdAndStatus(String subject, String versionId, String phaseId,
            Integer status);

    List<YxyResBook> findByIdInAndPhaseIdAndSubjectIdAndVersionId(List<String> list, String phase, String subject,
            String versionId);

    @Query(value = "SELECT DISTINCT subjectName,subjectId from YxyResBook WHERE phaseId = ?1 ORDER BY id asc")
    List<Object> findDistinctSubjectNameByPhaseId(String phaseId);

    YxyResBook findById(String id);

    @Query(value = " select t1.subjectId from YxyResBook t1 where t1.phaseId = ?1 and t1.status = 1 group by t1.subjectId")
    List<String> findSubjectIdByPhaseIdGroupBySubjectId(String phase);

    @Query(value = "select t1.versionId from YxyResBook t1 where t1.phaseId = ?1 and t1.subjectId = ?2 and t1.status = 1 group by t1.versionId")
    List<String> findVersionIdByPhaseIdAndSubjectIdGroupByVersionId(String phase, String subject);

    @Query(value = "select t1.versionId from YxyResBook t1 where t1.phaseId = ?1 and t1.subjectId = ?2 and t1.id in ?3 group by t1.versionId")
    List<String> findVersionIdByPhaseIdAndSubjectIdGroupByVersionId1(String phase, String subject, List<String> list2);

    @Query(value = " select t1.subjectId from YxyResBook t1 where t1.phaseId = ?1 and t1.id in ?2 and t1.status = 1 group by t1.subjectId")
    List<String> findSubjectIdByPhaseIdGroupBySubjectIdAndIdIn(String phase, List<String> ids);

    @Query(value = "select t1.semersterId from YxyResBook t1 where t1.phaseId = ?1 and t1.subjectId = ?2 and t1.id in ?3 and t1.versionId = ?4 group by t1.semersterId")
    List<String> findSemesters(String phase, String subject, List<String> ids, String versionId);

    YxyResBook findTop1BySubjectIdAndVersionIdAndPhaseIdAndStatus(String subject, String version, String phase, int i);

    YxyResBook findTop1BySubjectIdAndPhaseIdAndStatus(String subject, String phase, int i);

    @Query(value = " select distinct t1.versionId, t1.versionName from YxyResBook t1 where t1.subjectName = ?1 ")
    List<Object[]> getVersonName(String subjectName);

    @Query(value = " select distinct t1.semersterId, t1.semersterName from YxyResBook t1 where t1.subjectName = ?1 and t1.phaseName = ?2 and  t1.versionId = ?3 order by t1.semersterId ")
    List<Object[]> getVerson(String subjectName, String phaseName, String versionId);

    List<YxyResBook> findByIdIn(Set<String> bookIds);
}
